﻿using IMES.Common.Domain.GoldIngotModel;
using IMES.Common.Dtos;
using IMES.Common.Infrastructure;
using MediatR;

namespace GoldIngot.API.Application.Commands.CompletionOrderCommand
{
    public class CompletionOrderUpdateHandler : IRequestHandler<CompletionOrderUpdateCommand, Result>
    {
        private readonly IBaseRepositorys<CompletionOrder> orderRep;

        public CompletionOrderUpdateHandler(IBaseRepositorys<CompletionOrder> orderRep)
        {
            this.orderRep = orderRep;
        }

        public async Task<Result> Handle(CompletionOrderUpdateCommand request, CancellationToken cancellationToken)
        {
            var quest = new Result() { Code = (int)CodeEnum.success };

            var list = orderRep.GetAll().FirstOrDefault(x => x.CompletionId == request.CompletionId&&x.CompletionOrderStatus!=1&&x.CompletionOrderStatus!=0);

            if (list != null)
            {
                list.CompletionOrderStatus = request.CompletionOrderStatus;
            }

            quest.Code = orderRep.Update(list);
            var res= orderRep.GetAll().FirstOrDefault(x => x.CompletionId == request.CompletionId);
            if (res!= null)
            {
                quest.Data = res;
            }

            return quest;
        }

    }
}
